#include "MainWgt.h"
#include <QPushButton>
#include <QDebug>
#include <QPainter>

MainWgt::MainWgt(QWidget *parent): QWidget(parent)
{
	ui.setupUi(this);
	m_mediaCtrl = new vi::MediaCtroller;
	m_mediaCtrl->regiestCallback(this);
	m_mediaCtrl->regiestFunc(std::bind(&MainWgt::dataCallback,this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
	connect(ui.start, &QPushButton::clicked, this, [=] {
		m_mediaCtrl->connectServer(ui.lineEdit->text());
	});
}

MainWgt::~MainWgt()
{
}

void MainWgt::dataCallback(QString peerId, unsigned char* data, int width, int height)
{
	qDebug() << "peerId:" << peerId << ",width:" << width << ",height:" << height;
	for (auto diaplay: m_peerList){
		if (diaplay->getPeerId() == peerId) {
			diaplay->inputData(data, width, height);
			break;
		}
	}
}

void MainWgt::onCreateCanvas(QString peerId)
{
	Display* display = new Display(this);
	display->setPeerId(peerId);
	m_peerList.push_back(display);
	display->show();
	switch (m_peerList.size())
	{
	case 1:
		display->setGeometry(0,0,500,300);
		break;
	case 2:
		display->setGeometry(520,0,500,300);
		break;
	case 3:
		display->setGeometry(0, 320, 500, 300);
		break;
	case 4:
		display->setGeometry(520, 320, 500, 300);
		break;
	default:
		break;
	}

}
void MainWgt::onNewPeer(QString peerId)
{
	QMetaObject::invokeMethod(this, "onCreateCanvas", Qt::QueuedConnection,Q_ARG(QString,peerId));
}

